<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">采集来源</label>
                            <div class="layui-input-inline">
                                <select name="type" lay-verify="" lay-search="">
                                    <option value="MYMRO">固安捷</option>
                                    <!--<option value="JD">京东</option>-->
                                    <!--<option value="JDMALL">京东工业品</option>-->
                                </select>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">采集机器</label>
                            <div class="layui-input-inline">
                                <input type="text" name="flag" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">任务名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>



        <table class="layui-hide" id="collectsTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="build">生成上架任务</a>
        </script>

    </div>
</div>

<script>
    layui.use(['form', 'table','miniPage','element', 'util'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage,
            util = layui.util;

        form.render();

        /**
         * pagenum: 2
         productnum: 34
         remark: null
         skunum: 197
         */

        table.render({
            elem: '#collectsTableId',
            url: 'common/collects',
            where: {type: 'MYMRO'},
            cols: [[
                {field: 'title', width: 420, title: '采集名称',
                    templet: function(data){
                        return '<a href="' + data.url + '" target="_blank">'+ data.title +'</a>'
                    }
                },
                {field: 'status', width: 100, title: '状态', align: "center"},
                {field: 'pagenum', width: 100, title: '页数', align: "center"},
                {field: 'productnum', width: 100, title: '产品组', align: "center"},
                {field: 'skunum', width: 100, title: 'SKU数量', align: "center"},
                {field: 'updatetime', width: 240, title: '更新时间', align: "center", templet : "<div>{{layui.util.toDateString(d.updatetime, 'yyyy-MM-dd HH:mm:ss')}}</div>"},
                {field: 'flag', width: 100, title: '采集机器', align: "center"},
                {field: 'typename', width: 140, title: '数据来源', align: "center"},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"},
                {field: 'remark', width: 280, title: '备注'}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('collectsTableId', {
                page: {curr: 1 }
                , where: data.field
            }, 'data');

            return false;
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;

            debugger;

            if (obj.event === 'build') {

                delete data["updatetime"];

                $.ajax({
                    type: 'POST',
                    url: "common/collects/buildtask",
                    data: data,
                    success: function (result) {

                        var msg = "";
                        var success = false;
                        if ( !!result && result.code === 0 ) {
                            success = true;
                            msg = result["msg"] || "任务生成成功!";
                        } else if ( !!result ) {
                            msg = result["msg"] || "任务生成失败!";
                        }

                        layer.open({
                            type: 1
                            , title: false //不显示标题栏
                            , closeBtn: false
                            , area: '300px;'
                            , shade: 0.8
                            , id: 'LAY_layuipro' //设定一个id，防止重复弹出
                            , btn: ['确 定', '关 闭']
                            , btnAlign: 'c'
                            , moveType: 1 //拖拽模式，0或者1
                            , content: '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">你知道吗？亲！<br><br>' + msg + '</div>'
                            , success: function (layero) {
                                if ( success === true ) {
                                    var btn = layero.find('.layui-layer-btn');
                                    btn.find('.layui-layer-btn0').attr({
                                        href: 'index.html#/pages/shelvestask.html'
                                        , target: '_self'
                                    });
                                }
                            }
                        });

                    },
                    dataType: "json"
                });

            }

            // if (obj.event === 'edit') {
            //
            //     var content = miniPage.getHrefContent('page/table/add.html');
            //     var openWH = miniPage.getOpenWidthHeight();
            //
            //     var index = layer.open({
            //         title: '编辑用户',
            //         type: 1,
            //         shade: 0.2,
            //         maxmin:true,
            //         shadeClose: true,
            //         area: [openWH[0] + 'px', openWH[1] + 'px'],
            //         offset: [openWH[2] + 'px', openWH[3] + 'px'],
            //         content: content,
            //     });
            //     $(window).on("resize", function () {
            //         layer.full(index);
            //     });
            //     return false;
            // }
        });
    });
</script>